package MathTest.Question;

import MathTest.Calculate.Key;
import MathTest.MyTest;

import java.io.*;
import java.util.Scanner;

public class Test extends Question {
    File file = new File("题目.txt");
    int n = MyTest.n;
    int grade = MyTest.grade;
    int choice;                                 //运算符的个数
    int count;
    float right = 0;
    Random random = new Random();
    String s = "";
    Scanner in = new Scanner(System.in);
    public  void AddSub(){
        arithmetic(2);
        Read();
    }
    public void AddSubMultiDiv(){
        arithmetic(4);
        Read();
    }
    public void AddSubMultiDivBranket(){
        arithmetic(4);
        Read();
    }
    public void Fraction(){
        arithmetic1();
        Read1();
    }
    public void arithmetic(int stage){              //+-*÷()
        for(int i=1;i<=n;i++){
            choice = random.RandomNum(4)+1;
            count = 0;
            s = "";
            for (int j = 1; j <= choice + 1; j++) {        //产生题目，一共应该2*choice+1个元素
                int m = random.RandomNum(10);
                while(m==0)
                    m = random.RandomNum(10);
                s = s + m;                      //产生一个数字
                if (count < choice) {
                    s = s + " " + random.RandomChar(stage) + " ";             //在原基础上再产生字符
                    count++;
                }
            }
            if(grade>=3){                                  //产生括号
                s = random.RandomBranket(s,choice);
            }
            Write(s);
        }
    }
    public void arithmetic1(){             //真分数()
        for(int i=1;i<=n;i++){
            choice = random.RandomNum(4)+1;
            count = 0;
            String fraction = "";
            s = "";
            for (int j = 1; j <= choice + 1; j++) {        //产生题目，一共应该2*choice+1个元素
                int a = random.RandomNum(10) + 1;
                int b = random.RandomNum(10) + 1;
                int gcd = GCD(a, b);
                if (gcd == a) fraction = "1";            //分子分母相同
                else {
                    if (gcd != 1) {                         //可约分
                        a = a / gcd;
                        b = b / gcd;
                    }
                    if (a < b) fraction = a + "/" + b;
                    else fraction = b + "/" + a;
                }
                s = s + fraction;                      //产生十以内的个数字
                if (count < choice) {
                    s = s + " " + random.RandomChar(4) + " ";             //在原基础上再产生字符
                    count++;
                }
            }
            s = random.RandomBranket(s,choice);
            Write(s);
        }
    }
    public void Write(String s){
        try{                            //写入文件
            FileOutputStream out = new FileOutputStream(file,true);
            OutputStreamWriter write = new OutputStreamWriter(out,"gbk");
            BufferedWriter writer = new BufferedWriter(write);
            writer.write(s);
            writer.newLine();
            writer.close();
        }catch (IOException e){}
    }
    public void Read(){
        int i=1;
        try{                           //读取文件
            InputStreamReader read = new InputStreamReader(new FileInputStream(file),"gbk");
            BufferedReader reader = new BufferedReader(read);
            while((s=reader.readLine())!=null){
                MyTest.lan.Print("c");
                System.out.println(i+":");
                System.out.print(s+" = ");
                i++;
                int key = Key.Key(s);
                int result = in.nextInt();
                if(key==result){
                    MyTest.lan.Print("d");
                    System.out.print("\n");
                    right++;
                }
                else{
                    MyTest.lan.Print("e");
                    System.out.println(" "+key);
                }
            }
            System.out.print(String.format("%.2f", right / n * 100) + "%");
            read.close();
        }catch (IOException e){}
    }
    public void Read1(){
        int i=1;
        try{                           //读取文件
            InputStreamReader read = new InputStreamReader(new FileInputStream(file),"gbk");
            BufferedReader reader = new BufferedReader(read);
            while((s=reader.readLine())!=null){
                MyTest.lan.Print("c");
                System.out.println(i+":");
                System.out.print(s+" = ");
                i++;
                String key1 = Key.Key1(s);
                String result1 = in.next();
                if(key1.equals(result1)){
                    MyTest.lan.Print("d");
                    System.out.print("\n");
                    right++;
                }
                else{
                    MyTest.lan.Print("e");
                    System.out.println(" "+key1);
                }
            }
            System.out.print(String.format("%.2f", right / n * 100) + "%");
            read.close();
        }catch (IOException e){}
    }
    public static int GCD(int a,int b){                            //计算最大公因子
        int m = Math.abs(a);
        int n = Math.abs(b);
        while (m != n) {
            if (m > n)
                m -= n;
            else
                n -= m;
        }
        return m;
    }
}
